package demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 *  问题2：
	请按照小时+30分钟组装数据，进行转换，
	如把小时数据和小时对应的30分钟数据组装如下的Map，
	如<0130，23>，<0160，34>，<0230，12>，<0260，15>，以此类推，
	对结果进行打印，
	并对这个Map按照整数Value从大到小排序，返回排序后的Map，进行打印。
 */

public class HalfHoursDemo {
	public static void main(String[] args) throws Exception {
		//从data.txt读入数据放入list集合中
		ArrayList<String> list = getList();

		//处理list集合，获取小时、30分钟数据的集合
		ArrayList<String> hours = getHoursData(list);
		ArrayList<String> halfHoursData = getHalfData(list);
		//System.out.println(halfHoursData);

		//处理hours集合，创建符合条件的key值的集合
		ArrayList<String> key1 = new ArrayList<>();
		for(int i=0;i<16;i++){
			if(i%2==0){
				key1.add("0"+hours.get(i/2)+"30");
			}else{
				key1.add("0"+hours.get(i/2)+"60");
			}
		}
		
		//创建map集合，放入30分钟数据
		Map<String, String> map =new HashMap<String, String>();
		for(int i=0;i<key1.size();i++){
			map.put(key1.get(i), halfHoursData.get(i));
		}
		
		//打印输出30分钟数据
		System.out.println("半小时排序数据：");
		List<Map.Entry<String, String>> list1 = new ArrayList<>(map.entrySet());
		list1.sort((e1, e2) -> Integer.compare(Integer.parseInt(e2.getValue()), Integer.parseInt(e1.getValue())));
		for (Map.Entry<String, String> entry : list1) {  
			System.out.println("<" + entry.getKey() + "," + entry.getValue() + ">");  
		} 
	}

	//getList
	private static ArrayList<String> getList() throws Exception {
		ArrayList<String> list = new ArrayList<>();
		BufferedReader br = new BufferedReader(new FileReader("data.txt"));
		String line;  
		while ((line = br.readLine()) != null) {  
			list.add(line);
		} 
		br.close();
		//System.out.println(list);
		return list;
	}

	//getHoursData
	private static ArrayList<String> getHoursData(ArrayList<String> list) {
		ArrayList<String> hours = new ArrayList<>();
		String str=list.get(1);
		//分离";"
		for(int i=0;i<8;i++){
			String[] split= str.split(";");
			hours.add(split[i]);
		}
		//System.out.println(hours);
		return hours;
	}
	
	//getHalfData
	private static ArrayList<String> getHalfData(ArrayList<String> list) {
		ArrayList<String> halfHours1 = new ArrayList<>();
		ArrayList<String> halfHours = new ArrayList<>();
		//取30分钟字串
		String str=list.get(3);
		//分离";"
		for(int i=0;i<8;i++){
			String[] split= str.split(";");
			halfHours1.add(split[i]);
		}
		//取data
		Pattern pattern = Pattern.compile(":(\\d+),");  
		Matcher matcher = pattern.matcher(halfHours1.toString());  
		while (matcher.find()) {  
			String number = matcher.group(1);
			halfHours.add(number);
		}
		String[] split=halfHours1.toString().split(":");
		String last = split[16].substring(0,split[16].length()-1);
		halfHours.add(last);
		//处理数据，若为个位数，则前面加0
		for(int i=0;i<halfHours.size();i++){
			if(halfHours.get(i).length()==1){
				halfHours.set(i, "0".concat(halfHours.get(i)));
			}
		}
		//System.out.println(halfHours);
		return halfHours;
	}
}
